package com.txh.blog.controller.label;

import com.alibaba.druid.support.json.JSONUtils;
import com.txh.blog.exceprtion.exceptions.NullAttrException;
import com.txh.blog.model.Label;
import com.txh.blog.service.LabelService;
import com.txh.blog.util.ResultJsonUtils;
import com.txh.blog.vo.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("lable")
public class LabelController {

    @Autowired
    LabelService labelService;
    @RequestMapping("/list")
    public String lableList(Integer pageSize, Integer pageNum, Model model){
        Map<String,Object> page = labelService.getLabelList(pageNum,pageSize);
        model.addAttribute("page",page);
        return "/admin/blogTagList";
    }

    @RequestMapping("del")
    @ResponseBody
    public ResultJson deleteLabel(Long id){
        if (id == null){
            throw new NullAttrException();
        }
        labelService.deleteLabelById(id);
        return ResultJsonUtils.success(null);
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResultJson add(Label label){
        if(label == null){
            throw new NullAttrException();
        }
        labelService.addLabel(label);
        return ResultJsonUtils.success(label);
    }
}
